package com.wheel.autoconfigure.web.mvc.advice;/*
 * <h3>Title:wheel-spring-boot-starter-logging系统</h3>
 * <p>Copyright (c) 2020, wangnaifei@163.com. All rights reserved.</p>
 * @author wangnaifei@163.com
 */

import com.wheel.core.result.Result;
import com.wheel.core.utils.Log;
import org.slf4j.MDC;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.reflect.Method;

/**
 * <dt>Description:ResultResponseBodyAdvice</dt>
 * <dd>返回结果的ResponseBody统一拦截</dd>
 *
 * @author wangnaifei@163.com
 * @version 1.0 <i>2020/11/24 19:02</i>
 * @since jdk1.7+
 */
@ControllerAdvice
public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Result<?>> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        Method method = returnType.getMethod();
        if (method == null) {
            return false;
        }
        Class<?> returnType1 = returnType.getMethod().getReturnType();
        return returnType1.isAssignableFrom(Result.class);
    }

    @Override
    public Result<?> beforeBodyWrite(Result<?> body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return body;
    }
}
